/**
 * 马上提交控制器
 */
import tem from './index.html'
import './index.less'
import $_fn from '~plugins'
class soonApproveCtrl {
  constructor($scope,$rootScope,http,$ui,$state,$location,$timeout) {
    $scope.param = {}
    $scope.redErr = {}
    $scope.attaArr = [{attachImg:[]}]
    console.log('$ui', $location, $scope.params)
    $scope._interest = ($scope.params.apply_money*$scope.params.interest)/100
    //警告提示
    $scope.errorToast = function(text) {
      $ui.toast({text:text,type:'warning',position:'center',appendTo:'.wp-container'});
    }
    //成功提示
    $scope.successToast = function(text) {
        $ui.toast({text:text,type:'success',position:'center',appendTo:'.wp-container'});
    }
    $scope.cancel = function () {
      $rootScope.$uiDirectiveModal.close() //关闭方法
    }
    $scope.remHight = function(){                                                 //去除报错高亮
      for(var i in $scope.redErr){
        $scope.redErr[i] = false
      }
    }
    $scope.ok=function (state) {
      if(!$scope.param.remark){
        $scope.redErr.remarks=true
        $scope.errorToast('请填写审批意见')
        return
      }
      $scope.param.oid = $scope.params.oid
      $scope.param.typeState = state
      $scope.approvalUrl && ($scope.param.approvalUrl = $scope.approvalUrl)
      http.post('immediateApproval.do',$scope.param,function (data) {
        if(data.errcode == 1){
          $scope.successToast(data.errmsg)
          $scope.cancel()
          if($rootScope.refreshPage){
            $rootScope.refreshPage(1,{s_id:2,sIds:2})
          }
        }else{
          $scope.errorToast(data.errmsg || '提交失败！')
        }
      })
    }

    //图片预加载
    $scope.loadImage = $_fn.loadImage;
    //图片查看
    $scope.imgList = $_fn.imgList;

    $timeout(function () {
      $_fn.ueditorImg(function(data,_btn){
        console.log('ueditorImg',data,_btn)
        $scope.$apply(function () {
          if($scope.attaArr[0].attachImg.length > 0){
            $scope.attaArr[0].attachImg = $scope.attaArr[0].attachImg.concat(data)
          }else{
            $scope.attaArr[0].attachImg = data
          }
          $_fn.imgList(0, true)
          //JSON.s
          /* if($scope.attaArr[index].attachImg.length > 4 ){
            $scope.AttaSele[key].isImg = true   //判断图片是否多个有滚动条
            $scope.AttaSele[key].left = 0
          } */
        })
      })
      // $('#imgTerm').find('div').removeClass('webuploader-pick')
    },500)
    //删除图片方法
    $scope.deleImg= function(event,key){													//图片集删除方法
      $_fn.stopBOM(event)
      let rdx = parseInt(event.target.getAttribute('data-idx'))
      $scope.attaArr[0].attachImg.splice(rdx, 1)
      //判断图片是否多个有滚动条
      /* if($scope.attaArr[0].attachImg.length <= 4 ){
        $scope.AttaSele[key].isImg = false
        $scope.AttaSele[key].left = 0
      } */
  }
  //左右切换
  $scope._pre =function (key) {
    if($scope.AttaSele[key].left >= 0){
      $scope.AttaSele[key].left = 0
    } else {
      $scope.AttaSele[key].left += 71
    }
  }
  $scope._next =function (key) {
    if ((274-parseInt($('#gallery'+key).children().css('width'))) >= $scope.AttaSele[key].left){
      $scope.AttaSele[key].left = (274-parseInt($('#gallery'+key).children().css('width')))
    }else{
      $scope.AttaSele[key].left -= 71
    }
  }
    
  }
}


soonApproveCtrl.$inject = ['$scope','$rootScope','http','$ui','$state','$location','$timeout','$interval']
module.exports = angular.module('directive.soonApprove', []).directive('soonApprove', function() {
  return {
    restrict: 'E',
    template: tem,
    replace:true,
    scope: {
      params: '<'
    },
    controller: soonApproveCtrl,
    link:function(){
      $_fn.clickBind()
    }
  }
}).name